package com.tt.payee.filter.webFliters;

import com.tt.payee.business.BusinessException;
import com.tt.payee.business.BusinessStatus;
import com.tt.payee.enums.EnumUtils;
import com.tt.payee.enums.PlatformEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author ：Tang
 * @date ：Created in 2020/8/19 14:43
 * @description：
 * @modified By：
 * @version: $
 */
@Component
public class PlatformFilter implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {


        String path = request.getRequestURI();

        if (path.contains("/km-terminal-api/userOrderController/notify") || path.contains("/km-terminal-api/userOrderController/aliNotify")) {
            return true;
        }

        //获取platform
        String platform = request.getHeader("platform");
        if (
                StringUtils.isBlank(platform) ||
                        !StringUtils.isNumeric(platform) ||
                        EnumUtils.getEnumBycode(PlatformEnum.class, Integer.parseInt(platform)) == null
        ) {

            throw new BusinessException(BusinessStatus.ILLEGAL_REQUEST);
        }

        return true;

    }

}
